package com.doge;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import java.util.ArrayList;

public class FightScreen extends Stage implements Screen, InputProcessor
{
	public Main parent;
	SpriteBatch batch = new SpriteBatch();
	public TextureRegion battlescreen;
	
	Player player = null;
	Character enemy = null;
	
	public BitmapFont font = new BitmapFont();
	String statusText = null;
	
	int activeButton = 0;
	float delta = 0;
	
	boolean playerTurn;
	boolean battleDone;
	
	Stage stage;
	Screen screen;
	
	public FightScreen(Main parent, Player player){
		this.parent = parent;
		battlescreen = parent.textures.battleScreen;
		
		screen = parent.overworld;
		stage = parent.overworld;
		
		this.player = player;
	}
	
	public void setCharacters(Character enemy)
	{
		battleDone = false;
		this.enemy = enemy;
		
		
		playerTurn =  Math.random() < 0.5;
		if(!playerTurn) {
			//statusText = enemy.name + " has gained the upper hand.";
			fight();
		}
		
		//playerTurn = false;
	}
	
	@Override
	public void render(float delta) {
		
		this.delta += delta;
		if(this.delta < .0133337) return;
		this.delta = 0;
		
		batch.begin();
		batch.draw(battlescreen, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		batch.draw(enemy.textureRegion,(Gdx.graphics.getWidth()/2)-75,(Gdx.graphics.getHeight()-210),128,128);
		
		if(statusText != null)
		{
			font.draw(batch, statusText, Gdx.graphics.getWidth()/2 - font.getBounds(statusText).width/2, (Gdx.graphics.getHeight()/2)+font.getXHeight());
			//font.draw(batch, "Press Enter To Continue", Gdx.graphics.getWidth()/2 - font.getBounds("Press Enter To Continue").width/2,450+font.getXHeight());
		}
		
		if(playerTurn && !battleDone)
			for(int x = 0; x < player.battleActions.size(); ++x)
			{
				float padding = 20;
				float xpos = Gdx.graphics.getWidth()/2;
				float ypos = (15*x)+font.getXHeight()+(Gdx.graphics.getHeight()/5);
				String name = player.battleActions.get(x).name;
				
				//draw arrow
				if(x == activeButton)
				{
					batch.draw(parent.textures.arrowRight,xpos-70, 
							ypos-10, 
							64, 
							36);
				}

				font.setColor(Color.BLACK);
				if(player.battleActions.get(x).isLocked)
				{
					font.setColor(Color.GRAY);
				}
				//draw 'buttons'
				batch.draw(parent.textures.button, xpos-6,
						ypos,
						font.getBounds(name).width+12,
						font.getBounds(name).height+5);
				
				font.draw(batch, name, xpos, ypos+15);
			}
		
		if(battleDone)
		{
			float xpos = (Gdx.graphics.getWidth()/2)-50;
			float ypos = (Gdx.graphics.getHeight()/2)-50;
			String name = "Exit";
			batch.draw(parent.textures.button, xpos-6, ypos, font.getBounds(name).width+12, font.getBounds(name).height+5);
			font.draw(batch, name, xpos, ypos+15);
		}
		
		
		//draw hud
		font.draw(batch, "Health: " +player.health, 30, 530);
		font.draw(batch, "Energy: " +player.energy, 30, 510);
		
		font.draw(batch, "Health: " +enemy.health, 30, 430);
		font.draw(batch, "Energy: " +enemy.energy, 30, 410);
		batch.end();
	}
	
	public void fight()
	{
		BattleAction a;
		Character c;
		if((a  = getBattleAction()) == null)
		{
			return;
		}
		
		if(a.isLocking && playerTurn)
		{
			BattleAction locked;
			while((locked = enemy.battleActions.get((int)Math.random()*enemy.battleActions.size())).isLockable == false);
			
			locked.isLocked = true;
		}else if(a.isLocking)
		{
			if(player.battleActions.size() > 0)
			{
				BattleAction locked;
				while((locked = player.battleActions.get((int)Math.random()*player.battleActions.size())).isLockable == false);
				
				locked.isLocked = !locked.isLocked;
			}
		}
		
		
		if(playerTurn)
		{
			if((enemy.health -= a.damage) < 1)
			{
				playerWin();
			}
			c = player;
		}else{
			if((player.health -= a.damage) < 1)
			{
				playerDead();
			}
			c = enemy;
		}
		
		if(battleDone)
		{
			playerTurn = false;
			return;
		}
		
		statusText = c.name + " uses " + a.name + ". It heals for: " + a.healing + " and damages for: "+a.damage;
		
		c.health += a.healing;
		c.energy -= a.energy;
		if((c.energy += c.energyRegen) > 100) c.energy = 100;
		
		playerTurn = !playerTurn;
	}
	
	public void playerWin()
	{
		player.dogeCoins += enemy.dogeCoins;
		statusText = "VICTORY! YOU GAINED: " + enemy.dogeCoins + " Doge Coins";
		player.health = player.maxHealth;
		player.energy = 100;
		battleDone = true;
	}
	
	public void playerDead()
	{
		statusText = "You have died.";
		battleDone = true;
		
		EndScreen s = new EndScreen(true);
		screen = s;
		stage = s;
	}
	
	public BattleAction getBattleAction()
	{
		BattleAction a;
		
		if(!playerTurn)
		{
			while((a = enemy.getNextAction()).isLocked)
			{
				enemy.battleActions.remove(enemy.battleActions.indexOf(a));
				if(enemy.battleActions.size() == 0)
					playerWin();
			}
			return a;
		}
		
		a = player.battleActions.get(activeButton);
		
		if((player.energy - a.energy) < 0)
		{
			statusText = "NOT ENOUGH ENERGY!";
			return null;
		}
		if(a.isLocked)
		{
			statusText = "ABILITY IS LOCKED!";
			return null;
		}
		
		return a;
	}
	
	public boolean keyDown(int keycode) 
	{	
		switch(keycode)
		{
		case Keys.W:
			if(++activeButton > player.battleActions.size()-1 && playerTurn)
				activeButton = 0;
			return true;
		case Keys.S:
			if(--activeButton < 0 && playerTurn)
				activeButton = player.battleActions.size()-1;
			return true;
		case Keys.ENTER:
			if(playerTurn)
			{
				fight();
			}
			else if(battleDone)
				this.dispose();
			else
			{
				fight();
				//statusText = null;
			}
			return true;
		}
		
        return false;
    }

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() 
	{
		enemy = null;
		statusText = null;
		Gdx.input.setInputProcessor(stage);
		parent.setScreen(screen);
	}

}
